package com.hnu.highlynewapp.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hnu.highlynewapp.entity.ActivityEntity;
import com.hnu.highlynewapp.exception.BizCodeEnum;
import com.hnu.highlynewapp.utils.ValidationUtils;
import com.hnu.highlynewapp.vo.PublishActivityVo;
import com.hnu.highlynewapp.vo.PublishIdeaVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.hnu.highlynewapp.entity.BusinessIdeaEntity;
import com.hnu.highlynewapp.service.BusinessIdeaService;
import com.hnu.highlynewapp.utils.PageUtils;
import com.hnu.highlynewapp.utils.R;
import springfox.documentation.annotations.ApiIgnore;


/**
 * 企业意见
 *
 * @author hnu_worker
 * @email 825963704@qq.com
 * @date 2021-01-03 10:35:55
 */
@Api(tags = "企业意见api管理")
@RestController
@RequestMapping("/businessidea")
public class BusinessIdeaController {
    @Autowired
    private BusinessIdeaService businessIdeaService;

    /**
     * 企业发布企业意见
     */
    @ApiOperation("企业发布企业意见")
    @PostMapping("/publish")
    public R publish(@Validated @RequestBody PublishIdeaVo vo, BindingResult result) {
        if (result.hasErrors()) {
            Map<String, String> map = ValidationUtils.getErrors(result);
            return R.error(BizCodeEnum.INPUT_INVALID.getCode(), BizCodeEnum.INPUT_INVALID.getMessage()).put("data",map);
        } else {
            businessIdeaService.publish(vo);
            return R.ok();
        }
    }

    /**
     * 获取所有企业意见（时间降序）
     * @return
     */
    @ApiOperation("获取所有企业意见")
    @GetMapping("/getlist")
    public R getList() {
        List<BusinessIdeaEntity> list = businessIdeaService.list(new QueryWrapper<BusinessIdeaEntity>().orderByDesc("publish_time"));
        return R.ok().put("data", list);
    }

    /**
     * 根据id获取企业意见
     * @return
     */
    @ApiOperation("根据id获取企业意见")
    @GetMapping("/get/{ideaId}")
    public R getById(@PathVariable("ideaId") Long ideaId) {
        BusinessIdeaEntity businessIdeaEntity = businessIdeaService.getById(ideaId);
        if (businessIdeaEntity == null) {
            return R.error(BizCodeEnum.INFO_NOT_FOUND_EXCEPTION.getCode(), BizCodeEnum.INFO_NOT_FOUND_EXCEPTION.getMessage());
        }
        return R.ok().put("data", businessIdeaEntity);
    }

    /**
     * 根据角色id获取自己发布的企业意见
     * @return
     */
    @ApiOperation("根据角色id获取自己发布的企业意见")
    @GetMapping("/getlist/{roleId}")
    public R getListByRoleId(@ApiParam(value = "角色id",required = true) @PathVariable("roleId") Long roleId) {
        List<BusinessIdeaEntity> list = businessIdeaService.list(new QueryWrapper<BusinessIdeaEntity>().eq("business_id",roleId).orderByDesc("publish_time"));
        return R.ok().put("data", list);
    }

    /**
     * 列表
     */
    @ApiIgnore
    @RequestMapping("/list")
    //@RequiresPermissions("highlynewapp:businessidea:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = businessIdeaService.queryPage(params);

        return R.ok().put("page", page);

    }


    /**
     * 信息
     */
    @ApiIgnore
    @RequestMapping("/info/{businessIdeaId}")
    //@RequiresPermissions("highlynewapp:businessidea:info")
    public R info(@PathVariable("businessIdeaId") Long businessIdeaId){
		BusinessIdeaEntity businessIdea = businessIdeaService.getById(businessIdeaId);

        return R.ok().put("businessIdea", businessIdea);
    }

    /**
     * 保存
     */
    @ApiIgnore
    @RequestMapping("/save")
    //@RequiresPermissions("highlynewapp:businessidea:save")
    public R save(@RequestBody BusinessIdeaEntity businessIdea){
		businessIdeaService.save(businessIdea);

        return R.ok();
    }

    /**
     * 修改
     */
    @ApiIgnore
    @RequestMapping("/update")
    //@RequiresPermissions("highlynewapp:businessidea:update")
    public R update(@RequestBody BusinessIdeaEntity businessIdea){
		businessIdeaService.updateById(businessIdea);

        return R.ok();
    }

    /**
     * 删除
     */
    @ApiIgnore
    @RequestMapping("/delete")
    //@RequiresPermissions("highlynewapp:businessidea:delete")
    public R delete(@RequestBody Long[] businessIdeaIds){
		businessIdeaService.removeByIds(Arrays.asList(businessIdeaIds));

        return R.ok();
    }

}
